const { listBuckets, listFiles, listAllSubdirectories, deleteDirectory, deleteObject, createDirectory, getSTStoken } = require("../../controller/aliyunoss/ali-oss.controller");
const { sumbitPdfToWord, queryResult } = require('../../controller/aliyunoss/ali-oss.file.js')


const router = require("koa-router")()
router.prefix("/oss/aliyun");


//获取临时凭证
router.get("/get_sts_token_for_oss_upload", async (ctx, next) => {
    try {
        const data = await getSTStoken();
        console.log("路由接口获取到了", data)
        ctx.body = {
            code: 0,
            msg: "临时凭证获取成功",
            data: data
        };
    } catch (error) {
        ctx.body = {
            code: 1,
            msg: "获取临时凭证失败",
            data: null
        }
    }
})




//创建存储空间bucket
router.post("/createBucket", async (ctx, next) => {

})


//获取目录树结构
router.post("/listDir", async (ctx, next) => {
    try {
        const { dir = "images/" } = ctx.request.body;
        const res = await listAllSubdirectories({
            dir
        })
        ctx.body = {
            code: 0,
            msg: "成功",
            data: res
        };
    } catch (error) {

    }
})

//删除一个目录
router.post("/deleteDir", async (ctx, next) => {
    try {
        const { prefix } = ctx.request.body;
        const res = await deleteDirectory({ prefix })
        ctx.body = res
    } catch (error) {
        ctx.body = {
            code: 1,
            msg: "删除失败",
            data: null
        };
    }
})

//新建一个目录
router.post("/createDir", async (ctx, next) => {
    try {
        const {  dirName, parentDir } = ctx.request.body;
        const res = await createDirectory({  dirName, parentDir })
        ctx.body = res
    } catch (error) {
        ctx.body = {
            code: 1,
            msg: "创建目录失败",
            data: null
        };
    }
}
)

//删除一个object
router.post("/deleteObject", async (ctx, next) => {
    try {
        const { name } = ctx.request.body;
        const res = await deleteObject({ name: name })
        ctx.body = res
    } catch (error) {
        ctx.body = {
            code: 1,
            msg: "删除失败",
            data: null
        };
    }
})


//获取账号下的存储空间列表
router.get("/listBuckets", async (ctx, next) => {
    try {
        const res = await listBuckets()
    } catch (error) {

    }
})

//分页获取存储空间下的文件列表
router.post("/listFiles", async (ctx, next) => {
    try {
        const { prefix = 'images/', pageSize = 100, nextContinuationToken = null
        } = ctx.request.body;
        const data = await listFiles({ prefix, maxKeys: pageSize, nextContinuationToken })
        ctx.body = {
            code: 0,
            msg: "成功",
            data: data
        };
    } catch (error) {

    }
})

//文档转换格式
router.post("/documentConvert", async (ctx, next) => {
    try {
        const { files } = ctx.request;
        console.log("文档转换格式：", files,)
        const data = await sumbitPdfToWord(files);
        ctx.body = {
            code: 0,
            msg: "成功",
            data: data
        };
    } catch (error) {
        ctx.body = {
            code: 1,
            msg: "提交失败",
            data: null
        }
    }
})


//查询转换结果
router.get("/bindingResult", async (ctx, next) => {
    try {
        const { id } = ctx.request.query;
        const res = await queryResult(id);
        ctx.body = res
    } catch (error) {
        ctx.body = {
            code: 1,
            msg: "查询失败",
            data: null
        }
    }
})



module.exports = router;